﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using EntityObjects;

namespace DAL_V1.App_Code.DBBroker
{
    public class DisbursementDBBroker
    {
        private SSISDataSet ds;
        private SSISDataSetTableAdapters.DisbursementTableAdapter tda;
        private SSISDataSetTableAdapters.DisbursementDetailsTableAdapter detailstda;
        private SSISDataSetTableAdapters.UnCollectedTimeTableAdapter unCollectedtda;
        int index;
        private List<Disbursement> disb_list;
        private List<DisbursementDetails> disbDetails_list;
        private Disbursement disb;
        private DisbursementDetails disbDetails;
        private SSISDataSet.DisbursementDataTable disburs_td;
        private SSISDataSet.DisbursementDetailsDataTable disbursDetails_td;
        private SSISDataSet.UnCollectedTimeDataTable unCollected_td;

         //singleton
        private static bool instance_flag = false;
        private static DisbursementDBBroker instance;


        private DisbursementDBBroker()
        {
            ds = new SSISDataSet();
            tda = new SSISDataSetTableAdapters.DisbursementTableAdapter();
            detailstda = new SSISDataSetTableAdapters.DisbursementDetailsTableAdapter();
            unCollectedtda = new SSISDataSetTableAdapters.UnCollectedTimeTableAdapter();

            tda.Fill(ds.Disbursement);
            detailstda.Fill(ds.DisbursementDetails);
            unCollectedtda.FillUnCollectedTimeByDept(ds.UnCollectedTime);
        }

        public static DisbursementDBBroker getDisbursementDBBroker()
        {
            if (!instance_flag)
            {
                instance = new DisbursementDBBroker();
                instance_flag = true;

                return instance;
            }
            else
            {
                return instance;
            }
        }

        public List<Disbursement> selectAllDisbursement()
        {
            disb_list = new List<Disbursement>();
            for (int i = 0; i < ds.Disbursement.Rows.Count; i++)
            {
                disb = new Disbursement();
                disb.DisbursementId = ds.Disbursement[i].DisbursementId;
                disb.DeptId = ds.Disbursement[i].DeptId;
                disb.DisburseDate = ds.Disbursement[i].DisburseDate;
                disb.Status = ds.Disbursement[i].Status;
                disb.CollectionId = ds.Disbursement[i].CollectionId;
                disb.RepresentativeId = ds.Disbursement[i].RepresentativeId;

                //Detail
                disbDetails_list = selectDsibursementDetailsbyDisbursId(ds.Disbursement[i].DisbursementId);
                disb.DisburDetails_list = disbDetails_list;

                disb_list.Add(disb);
            }
            return disb_list;
        }

        public DataTable selectAllDisbursement_DT()
        {
            return ds.Disbursement;
        }

        public int addDisbursement(Disbursement disburs)
        {
            SSISDataSet.DisbursementRow newRow = ds.Disbursement.NewDisbursementRow();
            newRow.DisbursementId = disburs.DisbursementId;
            newRow.DeptId = disburs.DeptId;
            newRow.DisburseDate = disburs.DisburseDate;
            newRow.Status = disburs.Status;
            newRow.CollectionId = disburs.CollectionId;
            newRow.RepresentativeId = disburs.RepresentativeId;
                     
            ds.Disbursement.Rows.Add(newRow);
            return tda.Update(ds.Disbursement);
        }

        //private int getIndex(String Id)
        //{
        //    SSISDataSet.DisbursementRow row = ds.Disbursement.FindByDisbursementId(Id);
        //    return ds.Disbursement.Rows.IndexOf(row);
        //}

        private SSISDataSet.DisbursementRow getRowById(String Id)
        {
            return ds.Disbursement.FindByDisbursementId(Id);
        }

        public int updateDisbursement(Disbursement disburs, String Id)
        {
            SSISDataSet.DisbursementRow row = getRowById(Id);
            if (row != null)
            {
                row.DisbursementId = disburs.DisbursementId;
                row.Status = disburs.DeptId;
                row.DisburseDate = disburs.DisburseDate;
                row.Status = disburs.Status;
                row.CollectionId = disburs.CollectionId;
                row.RepresentativeId = disburs.RepresentativeId;
            }
                     
            return tda.Update(ds.Disbursement);
        }

        public int deleteDisbursement(String Id)
        {
            SSISDataSet.DisbursementRow row = getRowById(Id);
            if (row != null)
            {
                ds.Disbursement.Rows.Remove(row);
            }
            return tda.Update(ds.Disbursement);
        }

        public Disbursement selectDisbursement(String Id)
        {
            SSISDataSet.DisbursementRow r = getRowById(Id);
            if (r != null)
            {
                disb = new Disbursement();
                disb.DisbursementId = r.DisbursementId;
                disb.DeptId = r.DeptId;
                disb.DisburseDate = r.DisburseDate;
                disb.Status = r.Status;
                disb.CollectionId = r.CollectionId;
                disb.RepresentativeId = r.RepresentativeId;

                //Detail
                disbDetails_list = selectDsibursementDetailsbyDisbursId(r.DisbursementId);
                disb.DisburDetails_list = disbDetails_list;

                return disb;
            }
            else
            {
                return null;
            }
        }

        //AMMT
        public List<UnCollectedTime> getUnCollectedTime()
        {

            List<UnCollectedTime> unCollectedTime_list = new List<UnCollectedTime>();
            if (ds.UnCollectedTime.Rows.Count > 0)
            {
                for (int i = 0; i < ds.UnCollectedTime.Rows.Count; i++)
                {
                    UnCollectedTime unt = new UnCollectedTime();
                    unt.UnCollectedNo = ds.UnCollectedTime[i].UnCollectedTime;
                    unt.DeptId = ds.UnCollectedTime[i].DeptId;

                    unCollectedTime_list.Add(unt);
                }

                return unCollectedTime_list;
            }
            else
            {
                return null;
            }
        }

        ///////////////////////////////////////////////////////////////////////////

        //Disbursement Details
        public List<DisbursementDetails> selectAllDisbursementDetails()
        {
            disbDetails_list = new List<DisbursementDetails>();
            for (int i = 0; i < ds.DisbursementDetails.Rows.Count; i++)
            {
                disbDetails = new DisbursementDetails();
                disbDetails.DisbursementId = ds.DisbursementDetails[i].DisbursementId;
                disbDetails.ItemId = ds.DisbursementDetails[i].ItemId;
                disbDetails.ReceivedQty = ds.DisbursementDetails[i].ReceivedQty;
                disbDetails.RequestedQty = ds.DisbursementDetails[i].RequestedQty;
                disbDetails_list.Add(disbDetails);
            }
            return disbDetails_list;
        }

        public DataTable selectAllDisbursementDetails_DT()
        {
            return ds.DisbursementDetails;
        }

        public int addDisbursementDetails(DisbursementDetails disbursDetails)
        {
            SSISDataSet.DisbursementDetailsRow newRow = ds.DisbursementDetails.NewDisbursementDetailsRow();
            newRow.DisbursementId = disbursDetails.DisbursementId;
            newRow.ItemId = disbursDetails.ItemId;
            newRow.ReceivedQty = disbursDetails.ReceivedQty;
            newRow.RequestedQty = disbursDetails.RequestedQty;
   
            ds.DisbursementDetails.Rows.Add(newRow);
            return detailstda.Update(ds.DisbursementDetails);
        }

        //private int getIndex(String disbursId,String itemId)
        //{
        //    SSISDataSet.DisbursementDetailsRow row = ds.DisbursementDetails.FindByDisbursementIdItemId(disbursId,itemId);            
        //    return ds.DisbursementDetails.Rows.IndexOf(row);
        //}

        private SSISDataSet.DisbursementDetailsRow getRowById(String disbursId, String itemId)
        {
            return ds.DisbursementDetails.FindByDisbursementIdItemId(disbursId, itemId);
        }

        public int updateDisbursementDetails(DisbursementDetails disbursDetails, String disbursId, String itemId)
        {
            SSISDataSet.DisbursementDetailsRow row = getRowById(disbursId, itemId);
            if (row != null)
            {
                row.ReceivedQty = disbursDetails.ReceivedQty;
                row.RequestedQty = disbursDetails.RequestedQty;
            }

            return detailstda.Update(ds.DisbursementDetails);
        }

        public int deleteDisbursementDetails(String disbursId, String itemId)
        {
            SSISDataSet.DisbursementDetailsRow row = getRowById(disbursId,itemId);
            if (row != null)
            {
                ds.DisbursementDetails.Rows.Remove(row);
            }
            return detailstda.Update(ds.DisbursementDetails);
        }

        public DisbursementDetails selectDisbursementDetails(String disbursId,String itemId)
        {
            SSISDataSet.DisbursementDetailsRow r = getRowById(disbursId, itemId);
            if (r != null)
            {
                disbDetails = new DisbursementDetails();
                disbDetails.DisbursementId = r.DisbursementId;
                disbDetails.ItemId = r.ItemId;
                disbDetails.ReceivedQty = r.ReceivedQty;
                disbDetails.RequestedQty = r.RequestedQty;

                return disbDetails;
            }
            else
            {
                return null;
            }
        }

        //select by disbursement Id
        public List<DisbursementDetails> selectDsibursementDetailsbyDisbursId(String Id)
        {
            disbursDetails_td = detailstda.GetDataByDisbursId(Id);
            disbDetails_list = new List<DisbursementDetails>();
            if (disbursDetails_td != null)
            {
                for (int i = 0; i < disbursDetails_td.Rows.Count; i++)
                {
                    disbDetails = new DisbursementDetails();
                    disbDetails.DisbursementId = disbursDetails_td[i].DisbursementId;
                    disbDetails.ItemId = disbursDetails_td[i].ItemId;
                    disbDetails.ReceivedQty = disbursDetails_td[i].ReceivedQty;
                    disbDetails.RequestedQty = disbursDetails_td[i].RequestedQty;
                    disbDetails_list.Add(disbDetails);
                }

                return disbDetails_list;
            }
            else
            {
                return null;
            }
        }

        
    }
}
